var prefix = "/jzweb/eventresourceAllocation";
$(function () {
    getIdList();
    getYearList()
    $('#yubtn').hide()
    $('#fileProgress').hide()
    $('#jzEveMoId').change(() => {
        $('#fileProgress').hide()
        $('#zyname').text('名称')

        if ($('#jzEveMoId').val() != 1775402446075265026) {
            $('#yearbox').hide()
            $('#eventTypebox').hide()
            $('#year').val('')
            $('#eventType').val('')
        } else {
            $('#zyname').text('赛项名称')
            $('#yearbox').show()
            $('#eventTypebox').show()
            $('#eventType').val(0)
        }
        if ($('#jzEveMoId').val() == 1775402577172430850) {
            $('#grade').show()
            $('#grade').val(1)
            $('#gradebox').show()
        } else {
            $('#gradebox').hide()
            $('#grade').hide()
            $('#grade').val('')
        }
        if ($('#jzEveMoId').val() == 1775402630247153666) {
            $('#yearOlds').show()
            $('#suoShu').hide()
            $('#eventTitle').val('')
            $('#eventTitle').prop('disabled', true);
            $('#yearOld').prop('disabled', false);
        } else {
            $('#suoShu').show()
            $('#yearOlds').hide()
            $('#yearOld').val('')
            $('#jianjie').hide()
            $('#mp4url').val('')
            $('#eventTitle').prop('disabled', false);
            $('#yearOld').prop('disabled', true);
        }
        if ($('#jzEveMoId').val() == 1775402307172499457 || $('#jzEveMoId').val() == 1775402630247153666) {
            $('#jianjie').show()
        } else {
            $('#jianjie').hide()
        }
    })

});

$().ready(function () {
    validateRule();
});

$.validator.setDefaults({
    submitHandler: function () {
        save();
    }
});


layui.use(['form', 'upload', 'element'], function () {
    var form = layui.form;
    var layer = layui.layer;
    var element = layui.element;
    var upload = layui.upload;
    var loading;
    upload.render({
        elem: '#videobtn'
        , url: '/common/sysFile/ftpUpload'
        , accept: 'video', //视频
        exts: 'mp3|mp4'
        ,
        choose: function (res) {
            $("#mp4url").val('正在上传中，请稍后...');
            $("#mp4url").prop('disabled', true);
        },
        done: function (res) {
            console.log(res)
            $("#mp4url").val(res.data);
            $("#mp4url").prop('disabled', false);
        },
        progress: function (e, percent) {
            $("#fileProgress").show();
            element.progress('progressBar', e + '%');
            // if (e != 100) {
            //     index = layer.load(1, {
            //         shade: [0.1, '#fff'],//0.1透明度的白色背景
            //         content: '请稍候，正在上传文件...',
            //         success: function (layerContentStyle) {
            //             layerContentStyle.find('.layui-layer-content').css({
            //                 'padding-top': '35px',
            //                 'text-align': 'left',
            //                 'width': '300px'
            //             });
            //         }
            //     });
            // }
        },
    });
    //拖拽上传
    upload.render({
        elem: '#drag'
        , url: '/common/sysFile/upload' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
        // ,field:'picture'
        , before: function () {
            loading = layer.load({
                type: 3,
                content: '加载中...',
            });//在post之前定义loading
        }
        , done: function (res) {
            $('#img').val(res.data);
            console.log($('#img').val(), 'urldata')
            $('#icon').hide()
            $('#icontext').hide()
            $('#hr').hide()
            layer.close(loading);
            if (res.code == 0) {
                $('#yubtn').show()
                layer.msg('上传成功');
                layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', res.data);
                $("input[name='picture']").val(res.data)
            } else {
                layer.msg('上传失败,请检查');
            }
        }
    });

    form.verify({
        name: function (value) {
            if (!value) {
                return '请输入图片名称'
            }
        },
        picture: function (value) {
            if (!value) {
                return '请上传图片'
            }
        }
    })
});

// 获取赛项模版id列表
function getIdList() {
    $.ajax({
        url: prefix + "/getModelIds",
        type: "get",
        success: function (r) {
            if (r.data.length == 0) {
                $("#jzEveMoId").append('<option value="">暂无数据</option>');
            } else {
                $("#jzEveMoId").append('<option value="">请选择</option>');
                for (let key in r.data) {
                    var a = r.data[key]
                    $("#jzEveMoId").append("<option value='" + key + "'>" + a + "</option>");
                }
                // for (var i=0;i<r.data.length; i++) {
                //     var a= r.data[i]
                //     $("#jzEveMoId").append("<option value='"+r.data[i]+"'>"+a+"</option>");
                // }
            }
        }
    });
}

function imgYulan() {
    layer.photos({
        photos: {
            'anim': 6,
            "title": "Photos Demo",
            "start": 0,
            "data": [
                {
                    "alt": "layer",
                    "pid": 1,
                    "src": $('#img').val(),
                },
            ]
        }
    });
}

// 获取年份列表
function getYearList() {
    $.ajax({
        url: prefix + "/getYearList",
        type: "get",
        success: function (r) {
            if (r.data.length == 0) {
                $("#year").append('<option value="">暂无数据</option>');
            } else {
                $("#year").append('<option value="">请选择</option>');
                for (var i = 0; i < r.data.length; i++) {
                    var a = r.data[i]
                    $("#year").append("<option value='" + r.data[i] + "'>" + a + "</option>");
                }
            }
        }
    });
}

function save() {
    $('#submit').prop('disabled', true);
    $.ajax({
        cache: true,
        type: "POST",
        url: "/jzweb/eventresourceAllocation/save",
        data: $('#signupForm').serialize(),// 你的formid
        error: function (request) {
            parent.layer.alert("Connection error");
            $('#submit').prop('disabled', false);
        },
        success: function (data) {
            if (data.code == 0) {
                $('#submit').prop('disabled', false);
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
                $('#submit').prop('disabled', false);
            }

        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            name: {
                required: true
            },
            jzEveMoId: {
                required: true
            },
            year: {
                required: true
            },
            eventName: {
                required: true
            },
            eventTitle: {
                required: true
            },
            eventType: {
                required: true
            }
            ,
            grade: {
                required: true
            }
        },
        messages: {
            name: {
                required: icon + "请输入姓名"
            },
            jzEveMoId: {
                required: icon + "请选择赛项模板id"
            },
            year: {
                required: icon + "请选择所属年份"
            },
            eventName: {
                required: icon + "请输入名称"
            },
            eventTitle: {
                required: icon + '请填写该信息'
            },
            eventType: {
                required: icon + "请选择类型"
            },
            grade: {
                required: icon + "请选择等级"
            }
        }
    })
}